package com.xjtu.duansheli.controller;


import com.xjtu.duansheli.entity.ArticleInfo;
import com.xjtu.duansheli.entity.UserInfo;
import com.xjtu.duansheli.json.AjaxResponse;
import com.xjtu.duansheli.service.ArticleInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Slf4j
@RestController
@RequestMapping("/project/article-info")
@Api(value = "帖子信息管理模块", description = "这是帖子信息增删改查的模块")
public class ArticleInfoController {
    
    @Autowired
    private ArticleInfoService articleInfoService;
    
    @ApiOperation(value = "获取全部帖子", notes = "获取所有帖子的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getArticles", method = RequestMethod.GET)
    public AjaxResponse getAll(@RequestParam(value = "current") Integer current,
                               @RequestParam(value = "size") Integer size) {
        return AjaxResponse.success(articleInfoService.getAllArticle(current,size));
    }


    @ApiOperation(value = "根据id查找帖子", notes = "获取根据id查找到的帖子")
    @ApiImplicitParam(name = "aid", value = "帖子id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getArticleById", method = RequestMethod.GET)
    public AjaxResponse getUserById(@RequestParam(name = "aid") Long aid) {
        return AjaxResponse.success(articleInfoService.getArticleByid(aid));
    }

    @ApiOperation(value = "根据用户id查找它发布的帖子", notes = "根据用户id查找他发布的所有帖子")
    @ApiImplicitParam(name = "uid", value = "用户id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getArticleBysaleId", method = RequestMethod.GET)
    public AjaxResponse getArticleBysaleId(@RequestParam(name = "uid") Long uid) {
        return AjaxResponse.success(articleInfoService.getArticleByuid(uid));
    }


    @ApiOperation(value = "添加帖子信息", notes = "获取帖子信息并且添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "帖子标题", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "content", value = "帖子内容", required = true, paramType = "form", dataType = "String"),
    })
    @RequestMapping(value = "/addArticle", method = RequestMethod.POST)
    public AjaxResponse addArticle(@RequestParam(value = "title") String title,
                                   @RequestParam(value = "content") String content,
                                   HttpServletRequest request) {
        UserInfo user = (UserInfo) request.getSession().getAttribute("loginUser");
        ArticleInfo article = new ArticleInfo()
                .setTitle(title)
                .setContent(content)
                .setAuthorId(user.getUid());
        return AjaxResponse.success("数据库受影响行数->" + articleInfoService.addArticle(article));
    }

    @ApiOperation(value = "修改帖子信息", notes = "录入新的帖子信息并且修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aid", value = "帖子id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "title", value = "帖子标题", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "content", value = "帖子内容", required = true, paramType = "form", dataType = "String"),
    })
    @RequestMapping(value = "/updateArticle", method = RequestMethod.PUT)
    public AjaxResponse updateArticle(@RequestParam(value = "title") String title,
                                      @RequestParam(value = "content") String content,
                                      @RequestParam(value = "aid") Long aid) {

        ArticleInfo article = new ArticleInfo()
                .setAid(aid)
                .setContent(content)
                .setTitle(title);
        return AjaxResponse.success("数据库受影响行数->" + articleInfoService.updateArticle(article));
    }

    @ApiOperation(value = "删除帖子信息", notes = "从数据库中删除某个帖子")
    @ApiImplicitParam(name = "aid", value = "帖子id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteArticle", method = RequestMethod.DELETE)
    public AjaxResponse deleteArticle(@RequestParam(value = "aid") Long aid) {
        return AjaxResponse.success("数据库受影响行数->" + articleInfoService.deleteArticle(aid));
    }

    @ApiOperation(value = "帖子总数量", notes = "获取数据库中帖子总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(articleInfoService.getTotal());
    }
    
}

